package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.MetodoPagoDAO;
import com.bit.cfdi.web.domain.MetodoPago;
import com.bit.cfdi.web.domain.Negocio;

@Repository
public class MetodoPagoDAOImpl extends HibernateDaoSupport implements MetodoPagoDAO {

	@Autowired public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override 
	@Transactional public List<MetodoPago> getAll() throws DataAccessException {
		List<MetodoPago> metodoPago = getHibernateTemplate().find(
				"from MetodoPago where trim(descripcion)<>\'\'");
		
		return null!=metodoPago?metodoPago: null;
	}

	@SuppressWarnings("unchecked")
	@Override 
	@Transactional 
		public MetodoPago getByDescription(final String description) 
			throws DataAccessException {
		
		String query = "from MetodoPago as m where m.descripcion = ?";
		List<MetodoPago> metodoPago = getHibernateTemplate().find(query, description);		
		return metodoPago != null && !metodoPago.isEmpty() ? metodoPago.get(0) : null;
	}

	@Override 
	@Transactional public void save(final MetodoPago metodoPago) throws DataAccessException {
		getHibernateTemplate().save(metodoPago);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<MetodoPago> getAllDefault() throws DataAccessException {
		String query = "from MetodoPago as m " +
				"where m.negocio = null";
		List<MetodoPago> metodoPago = getHibernateTemplate().find(query);		
		return metodoPago;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<MetodoPago> getByNegocio(Negocio negocio) throws DataAccessException {
		String query = "from MetodoPago as m " +
				"where m.negocio = ?";
		List<MetodoPago> metodoPago = getHibernateTemplate().find(query, negocio);		
		return metodoPago;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public MetodoPago getByDescriptionDefatul(String description) throws DataAccessException {
		String query = "from MetodoPago as m " +
				"where m.descripcion = ? " +
				"and m.negocio = null";
		List<MetodoPago> metodoPago = getHibernateTemplate().find(query, description);		
		return metodoPago != null && !metodoPago.isEmpty() ? metodoPago.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public MetodoPago getByDescriptionNegocio(
			String description, Negocio negocio) throws DataAccessException {
		String query = "from MetodoPago as m " +
				"where m.descripcion = ? " +
				"and m.negocio = ?";
		List<MetodoPago> metodoPago = getHibernateTemplate().find(
				query, description, negocio);		
		return metodoPago != null && !metodoPago.isEmpty() ? metodoPago.get(0) : null;
	}

}